//this is for slave threads!
#ifndef  __CAS
#define __CAS

#include "task.h"

/*
The "val" version returns the contents of
     `*PTR' before the operation.
These builtins perform an atomic compare and swap.  That is, if
     the current value of `*PTR' is OLDVAL, then write NEWVAL into
     `*PTR'.
/////////////////
comments on assembly opcodes
about mov, 
mov from, to
    see true branch
           mov      %7,%1\n"
           stw      %1,%4\n"
about stw, ldi
 ldi  dest_reg, mem_addr
 stw  src_reg, mem_addr

*/

//volatile int __my_lock__=0;
/*struct task*  __sync_val_compare_and_swap(volatile int* my_lock
                       ,struct task  **mem
                       , struct task*  old
                       , struct task*  new)
{
  unsigned int __addr,__cmp;
  unsigned int  __tmpi;
  struct task* __prev, *__tmpp;
  __asm__ __volatile__(
      "        memb\n"
      "        ldi	%3,%6\n"
      "1:      faaw     %2,0(%3)\n"
      "        bne      %2,1b\n"
      "        memb\n"
      "        ldl      %0,%5\n"
      "        cmpeq    %0,%7,%1\n"
      "        beq      %1,2f\n"
      "        mov      %8,%4\n"
      "        stl      %4,%5\n"
      "        mov      1,%1\n"
      "2:      memb\n"
      "        mov      %0,%4\n"
      "        stw      $31,0(%3)\n"
      "        memb\n"
      : "=&r"  (__prev),
        "=&r"  (__cmp),
        "=&r"  (__tmpi),
        "=&r"  (__addr),
        "=&r"  (__tmpp)
      : "m" (*(mem)),
        "m" (*my_lock),
        "Ir" (old),
        "Ir" (new)
      : "memory");
    return __tmpp;
}
*/


#endif
